package dcc.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dcc.mbg.dao.BaseInfoMapper;
import dcc.mbg.dao.CountrySidePracticalMapper;
import dcc.mbg.dao.FolkSpecialPeopleMapper;
import dcc.mbg.dao.HainanChineseMapper;
import dcc.mbg.dao.HighProfessionalQualificationMapper;
import dcc.mbg.dao.MAcademicOrganizationAndSocialGroupMapper;
import dcc.mbg.dao.MActivityMapper;
import dcc.mbg.dao.MAttendMeetingAndServiceMapper;
import dcc.mbg.dao.MAttendPoliticalGroupMapper;
import dcc.mbg.dao.MEnterpriseOperationMapper;
import dcc.mbg.dao.MFamilyMapper;
import dcc.mbg.dao.MProductionAndManagementMapper;
import dcc.mbg.dao.MProfessionalSkillServiceMapper;
import dcc.mbg.dao.MResumeMapper;
import dcc.mbg.dao.MStoryMapper;
import dcc.mbg.dao.MSubtableMapper;
import dcc.mbg.dao.MTrainingStudyMapper;
import dcc.mbg.dao.OperationManagementMapper;
import dcc.mbg.dao.OutsideProvinceExpertsMapper;
import dcc.mbg.dao.ProfessionalSkillMapper;
import dcc.mbg.dao.SocialWorkMapper;
import dcc.mbg.dao.UserMapper;
import dcc.mbg.model.BaseInfo;
import dcc.mbg.model.BaseInfoExample;
import dcc.mbg.model.CountrySidePractical;
import dcc.mbg.model.CountrySidePracticalExample;
import dcc.mbg.model.FolkSpecialPeople;
import dcc.mbg.model.FolkSpecialPeopleExample;
import dcc.mbg.model.HainanChinese;
import dcc.mbg.model.HainanChineseExample;
import dcc.mbg.model.HighProfessionalQualification;
import dcc.mbg.model.HighProfessionalQualificationExample;
import dcc.mbg.model.MAcademicOrganizationAndSocialGroup;
import dcc.mbg.model.MAcademicOrganizationAndSocialGroupExample;
import dcc.mbg.model.MActivity;
import dcc.mbg.model.MActivityExample;
import dcc.mbg.model.MAttendMeetingAndService;
import dcc.mbg.model.MAttendMeetingAndServiceExample;
import dcc.mbg.model.MAttendPoliticalGroup;
import dcc.mbg.model.MAttendPoliticalGroupExample;
import dcc.mbg.model.MEnterpriseOperation;
import dcc.mbg.model.MEnterpriseOperationExample;
import dcc.mbg.model.MFamily;
import dcc.mbg.model.MFamilyExample;
import dcc.mbg.model.MProductionAndManagement;
import dcc.mbg.model.MProductionAndManagementExample;
import dcc.mbg.model.MProfessionalSkillService;
import dcc.mbg.model.MProfessionalSkillServiceExample;
import dcc.mbg.model.MResume;
import dcc.mbg.model.MResumeExample;
import dcc.mbg.model.MStory;
import dcc.mbg.model.MStoryExample;
import dcc.mbg.model.MSubtable;
import dcc.mbg.model.MSubtableExample;
import dcc.mbg.model.MTrainingStudy;
import dcc.mbg.model.MTrainingStudyExample;
import dcc.mbg.model.OperationManagement;
import dcc.mbg.model.OperationManagementExample;
import dcc.mbg.model.OutsideProvinceExperts;
import dcc.mbg.model.OutsideProvinceExpertsExample;
import dcc.mbg.model.ProfessionalSkill;
import dcc.mbg.model.ProfessionalSkillExample;
import dcc.mbg.model.SocialWork;
import dcc.mbg.model.SocialWorkExample;
import dcc.mbg.model.User;
import dcc.mbg.model.UserExample;

@Service
public class CommonService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private BaseInfoMapper baseInfoMapper;

	@Autowired
	private OperationManagementMapper operationManagementMapper;

	@Autowired
	private CountrySidePracticalMapper countrySidePracticalMapper;

	@Autowired
	private HainanChineseMapper hainanChineseMapper;

	@Autowired
	private ProfessionalSkillMapper professionalSkillMapper;

	@Autowired
	private SocialWorkMapper socialWorkMapper;

	@Autowired
	private FolkSpecialPeopleMapper folkSpecialPeopleMapper;

	@Autowired
	private HighProfessionalQualificationMapper highProfessionalQualificationMapper;

	@Autowired
	private OutsideProvinceExpertsMapper outsideProvinceExpertsMapper;

	public User checkUser(User record) {
		UserExample e = new UserExample();
		e.createCriteria().andUserNameEqualTo(record.getUserName())
				.andPasswordEqualTo(record.getPassword());
		List<User> list = userMapper.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public BaseInfo getBaseInfoByShiBieHaoAndLeiBie(BaseInfo record) {
		BaseInfoExample e = new BaseInfoExample();
		e.createCriteria().andShiBieHaoEqualTo(record.getShiBieHao())
				.andLeiBieEqualTo(record.getLeiBie());
		List<BaseInfo> list = baseInfoMapper.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public boolean isExistsByShiBieHao(String shiBieHao) {
		BaseInfoExample e = new BaseInfoExample();
		e.createCriteria().andShiBieHaoEqualTo(shiBieHao);
		List<BaseInfo> list = baseInfoMapper.selectByExample(e);
		return !list.isEmpty();
	}

	public void deleteMSubtable(int id) {
		this.msubtableMapper.deleteByPrimaryKey(id);
	}

	public void deleteMActivity(int id) {
		this.mactivityMapper.deleteByPrimaryKey(id);
	}

	public void deleteMResume(int id) {
		this.mresumeMapper.deleteByPrimaryKey(id);
	}

	public void deleteMTrainingStudy(int id) {
		this.mtrainingStudyMapper.deleteByPrimaryKey(id);
	}

	public void deleteMAttendMeetingAndService(int id) {
		this.mattendMeetingAndServiceMapper.deleteByPrimaryKey(id);
	}

	public void deleteMAcademicOrganizationAndSocialGroup(int id) {
		this.macademicOrganizationAndSocialGroupMapper.deleteByPrimaryKey(id);
	}

	public void deleteMProfessionalSkillService(int id) {
		this.mprofessionalSkillServiceMapper.deleteByPrimaryKey(id);
	}

	public void deleteMFamily(int id) {
		this.mfamilyMapper.deleteByPrimaryKey(id);
	}

	public void deleteMStory(int id) {
		this.mstoryMapper.deleteByPrimaryKey(id);
	}

	public void deleteMProductionAndManagement(int id) {
		this.mproductionAndManagementMapper.deleteByPrimaryKey(id);
	}

	public void deleteMAttendPoliticalGroup(int id) {
		this.mattendPoliticalGroupMapper.deleteByPrimaryKey(id);
	}

	public void deleteMEnterpriseOperation(int id) {
		this.menterpriseOperationMapper.deleteByPrimaryKey(id);
	}

	@Transactional(rollbackFor = Exception.class)
	public int saveOperationManagement(
			BaseInfo baseInfo,
			OperationManagement operationManagement,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MSubtable> listB4,
			List<MResume> mresumeList,
			List<MTrainingStudy> mtrainingStudyList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MProfessionalSkillService> mprofessionalSkillServiceList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			operationManagement.setBaseId(id);
			operationManagementMapper.insert(operationManagement);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);
			insertMSubtable(id, listB4);

			insertMResume(id, mresumeList);
			insertMTrainingStudy(id, mtrainingStudyList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMProfessionalSkillService(id, mprofessionalSkillServiceList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);

			return 1;
		} else {
			// TODO update
			operationManagement.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			operationManagementMapper
					.updateByPrimaryKeySelective(operationManagement);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);
			updateMSubtable(id, listB4);

			updateMResume(id, mresumeList);
			updateMTrainingStudy(id, mtrainingStudyList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMProfessionalSkillService(id, mprofessionalSkillServiceList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}
	}

	// 专业技术save()
	@Transactional(rollbackFor = Exception.class)
	public int saveProfessionalSkill(
			BaseInfo baseInfo,
			ProfessionalSkill professionalSkill,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MSubtable> listB4,
			List<MSubtable> listB5,
			List<MResume> mresumeList,
			List<MTrainingStudy> mtrainingStudyList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MProfessionalSkillService> mprofessionalSkillServiceList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			professionalSkill.setBaseId(id);
			professionalSkillMapper.insert(professionalSkill);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);
			insertMSubtable(id, listB4);
			insertMSubtable(id, listB5);

			insertMResume(id, mresumeList);
			insertMTrainingStudy(id, mtrainingStudyList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMProfessionalSkillService(id, mprofessionalSkillServiceList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);

			return 1;
		} else {
			// TODO update
			professionalSkill.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			professionalSkillMapper
					.updateByPrimaryKeySelective(professionalSkill);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);
			updateMSubtable(id, listB4);
			updateMSubtable(id, listB5);

			updateMResume(id, mresumeList);
			updateMTrainingStudy(id, mtrainingStudyList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMProfessionalSkillService(id, mprofessionalSkillServiceList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}
	}

	// 高技能人才save()
	@Transactional(rollbackFor = Exception.class)
	public int saveHighProfessionalQualification(
			BaseInfo baseInfo,
			HighProfessionalQualification highProfessionalQualification,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MSubtable> listB4,
			List<MResume> mresumeList,
			List<MTrainingStudy> mtrainingStudyList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MProfessionalSkillService> mprofessionalSkillServiceList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			highProfessionalQualification.setBaseId(id);
			highProfessionalQualificationMapper
					.insert(highProfessionalQualification);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);
			insertMSubtable(id, listB4);

			insertMResume(id, mresumeList);
			insertMTrainingStudy(id, mtrainingStudyList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMProfessionalSkillService(id, mprofessionalSkillServiceList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);

			return 1;
		} else {
			// TODO update
			highProfessionalQualification.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			highProfessionalQualificationMapper
					.updateByPrimaryKeySelective(highProfessionalQualification);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);
			updateMSubtable(id, listB4);

			updateMResume(id, mresumeList);
			updateMTrainingStudy(id, mtrainingStudyList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMProfessionalSkillService(id, mprofessionalSkillServiceList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}
	}

	// 社会工作人才save()
	@Transactional(rollbackFor = Exception.class)
	public int saveSocialWork(
			BaseInfo baseInfo,
			SocialWork socialWork,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB4,
			List<MResume> mresumeList,
			List<MTrainingStudy> mtrainingStudyList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MProfessionalSkillService> mprofessionalSkillServiceList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			socialWork.setBaseId(id);
			socialWorkMapper.insert(socialWork);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB4);

			insertMResume(id, mresumeList);
			insertMTrainingStudy(id, mtrainingStudyList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMProfessionalSkillService(id, mprofessionalSkillServiceList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);

			return 1;
		} else {
			// TODO update
			socialWork.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			socialWorkMapper.updateByPrimaryKeySelective(socialWork);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB4);

			updateMResume(id, mresumeList);
			updateMTrainingStudy(id, mtrainingStudyList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMProfessionalSkillService(id, mprofessionalSkillServiceList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}
	}

	// 民间特殊人士save()
	@Transactional(rollbackFor = Exception.class)
	public int saveFolkSpecialPeople(
			BaseInfo baseInfo,
			FolkSpecialPeople folkSpecialPeople,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MResume> mresumeList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			folkSpecialPeople.setBaseId(id);
			folkSpecialPeopleMapper.insert(folkSpecialPeople);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);

			insertMResume(id, mresumeList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);
			return 1;
		} else {
			// TODO update
			folkSpecialPeople.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			folkSpecialPeopleMapper
					.updateByPrimaryKeySelective(folkSpecialPeople);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);

			updateMResume(id, mresumeList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}
	}

	// 省外优秀专家save()
	@Transactional(rollbackFor = Exception.class)
	public int saveOutsideProvinceExperts(
			BaseInfo baseInfo,
			OutsideProvinceExperts outsideProvinceExperts,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MSubtable> listB4,
			List<MSubtable> listB5,
			List<MSubtable> listB6,
			List<MResume> mresumeList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			outsideProvinceExperts.setBaseId(id);
			outsideProvinceExpertsMapper.insert(outsideProvinceExperts);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);
			insertMSubtable(id, listB4);
			insertMSubtable(id, listB5);
			insertMSubtable(id, listB6);

			insertMResume(id, mresumeList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);
			return 1;
		} else {
			// TODO update
			outsideProvinceExperts.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			outsideProvinceExpertsMapper
					.updateByPrimaryKeySelective(outsideProvinceExperts);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);
			updateMSubtable(id, listB4);
			updateMSubtable(id, listB5);
			updateMSubtable(id, listB6);

			updateMResume(id, mresumeList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}
	}

	// --------------------------------------------------------------------------------------------

	@Autowired
	private MAcademicOrganizationAndSocialGroupMapper macademicOrganizationAndSocialGroupMapper;

	@Autowired
	private MActivityMapper mactivityMapper;

	@Autowired
	private MAttendMeetingAndServiceMapper mattendMeetingAndServiceMapper;

	@Autowired
	private MFamilyMapper mfamilyMapper;

	@Autowired
	private MProfessionalSkillServiceMapper mprofessionalSkillServiceMapper;

	@Autowired
	private MResumeMapper mresumeMapper;

	@Autowired
	private MStoryMapper mstoryMapper;

	@Autowired
	private MSubtableMapper msubtableMapper;

	@Autowired
	private MTrainingStudyMapper mtrainingStudyMapper;

	@Autowired
	private MProductionAndManagementMapper mproductionAndManagementMapper;

	@Autowired
	private MAttendPoliticalGroupMapper mattendPoliticalGroupMapper;

	@Autowired
	private MEnterpriseOperationMapper menterpriseOperationMapper;

	// --------------------------------------------------------------------------------------------
	/*
	 * Insert
	 */
	private void insertMStory(Integer baseId, List<MStory> mstoryList) {
		for (MStory record : mstoryList) {
			record.setBaseId(baseId);
			mstoryMapper.insertSelective(record);
		}
	}

	private void insertMActivity(Integer baseId, List<MActivity> mactivityList) {
		for (MActivity record : mactivityList) {
			record.setBaseId(baseId);
			mactivityMapper.insertSelective(record);
		}
	}

	private void insertMFamily(Integer baseId, List<MFamily> mfamilyList) {
		for (MFamily record : mfamilyList) {
			record.setBaseId(baseId);
			mfamilyMapper.insertSelective(record);
		}
	}

	private void insertMProfessionalSkillService(Integer baseId,
			List<MProfessionalSkillService> mprofessionalSkillServicelist) {
		for (MProfessionalSkillService record : mprofessionalSkillServicelist) {
			record.setBaseId(baseId);
			mprofessionalSkillServiceMapper.insertSelective(record);
		}
	}

	private void insertMAcademicOrganizationAndSocialGroup(
			Integer baseId,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGrouplist) {
		for (MAcademicOrganizationAndSocialGroup record : macademicOrganizationAndSocialGrouplist) {
			record.setBaseId(baseId);
			macademicOrganizationAndSocialGroupMapper.insertSelective(record);
		}
	}

	private void insertMAttendMeetingAndService(Integer baseId,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList) {
		for (MAttendMeetingAndService record : mattendMeetingAndServiceList) {
			record.setBaseId(baseId);
			mattendMeetingAndServiceMapper.insertSelective(record);
		}
	}

	private void insertMTrainingStudy(Integer baseId,
			List<MTrainingStudy> mtrainingStudyList) {
		for (MTrainingStudy record : mtrainingStudyList) {
			record.setBaseId(baseId);
			mtrainingStudyMapper.insertSelective(record);
		}
	}

	private void insertMResume(Integer baseId, List<MResume> mresumeList) {
		for (MResume record : mresumeList) {
			record.setBaseId(baseId);
			mresumeMapper.insertSelective(record);
		}
	}

	private void insertMSubtable(Integer baseId, List<MSubtable> msubtableList) {
		for (MSubtable record : msubtableList) {
			record.setBaseId(baseId);
			msubtableMapper.insertSelective(record);
		}
	}

	private void insertMProductionAndManagement(Integer baseId,
			List<MProductionAndManagement> mproductionAndManagementList) {
		for (MProductionAndManagement record : mproductionAndManagementList) {
			record.setBaseId(baseId);
			mproductionAndManagementMapper.insertSelective(record);
		}
	}

	private void insertMEnterpriseOperation(Integer baseId,
			List<MEnterpriseOperation> menterpriseOperationList) {
		for (MEnterpriseOperation record : menterpriseOperationList) {
			record.setBaseId(baseId);
			menterpriseOperationMapper.insertSelective(record);
		}
	}

	private void insertMAttendPoliticalGroup(Integer baseId,
			List<MAttendPoliticalGroup> mattendPoliticalGroupList) {
		for (MAttendPoliticalGroup record : mattendPoliticalGroupList) {
			record.setBaseId(baseId);
			mattendPoliticalGroupMapper.insertSelective(record);
		}
	}

	// --------------------------------------------------------------------------------------------
	/*
	 * Update Update 之前需要检测id，判断是更新还是插入 Update 之前需要检测id，判断是更新还是插入
	 */

	private void updateMStory(Integer baseId, List<MStory> mstoryList) {

		List<MStory> newList = new ArrayList<MStory>();
		for (MStory record : mstoryList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mstoryMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMStory(baseId, newList);
	}

	private void updateMActivity(Integer baseId, List<MActivity> mactivityList) {
		List<MActivity> newList = new ArrayList<MActivity>();
		for (MActivity record : mactivityList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mactivityMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMActivity(baseId, newList);
	}

	private void updateMFamily(Integer baseId, List<MFamily> mfamilyList) {
		List<MFamily> newList = new ArrayList<MFamily>();
		for (MFamily record : mfamilyList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mfamilyMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMFamily(baseId, newList);
	}

	private void updateMProfessionalSkillService(Integer baseId,
			List<MProfessionalSkillService> mprofessionalSkillServicelist) {
		List<MProfessionalSkillService> newList = new ArrayList<MProfessionalSkillService>();
		for (MProfessionalSkillService record : mprofessionalSkillServicelist) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mprofessionalSkillServiceMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMProfessionalSkillService(baseId, newList);
	}

	private void updateMAcademicOrganizationAndSocialGroup(
			Integer baseId,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGrouplist) {
		List<MAcademicOrganizationAndSocialGroup> newList = new ArrayList<MAcademicOrganizationAndSocialGroup>();
		for (MAcademicOrganizationAndSocialGroup record : macademicOrganizationAndSocialGrouplist) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				macademicOrganizationAndSocialGroupMapper
						.updateByPrimaryKey(record);
			}
		}
		this.insertMAcademicOrganizationAndSocialGroup(baseId, newList);
	}

	private void updateMAttendMeetingAndService(Integer baseId,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList) {
		List<MAttendMeetingAndService> newList = new ArrayList<MAttendMeetingAndService>();
		for (MAttendMeetingAndService record : mattendMeetingAndServiceList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mattendMeetingAndServiceMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMAttendMeetingAndService(baseId, newList);
	}

	private void updateMTrainingStudy(Integer baseId,
			List<MTrainingStudy> mtrainingStudyList) {
		List<MTrainingStudy> newList = new ArrayList<MTrainingStudy>();
		for (MTrainingStudy record : mtrainingStudyList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mtrainingStudyMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMTrainingStudy(baseId, newList);
	}

	private void updateMResume(Integer baseId, List<MResume> mresumeList) {
		List<MResume> newList = new ArrayList<MResume>();
		for (MResume record : mresumeList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mresumeMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMResume(baseId, newList);
	}

	private void updateMSubtable(Integer baseId, List<MSubtable> msubtableList) {
		List<MSubtable> newList = new ArrayList<MSubtable>();
		for (MSubtable record : msubtableList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				msubtableMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMSubtable(baseId, newList);
	}

	private void updateMProductionAndManagement(Integer baseId,
			List<MProductionAndManagement> mproductionAndManagementList) {
		List<MProductionAndManagement> newList = new ArrayList<MProductionAndManagement>();
		for (MProductionAndManagement record : mproductionAndManagementList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mproductionAndManagementMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMProductionAndManagement(baseId, newList);
	}

	private void updateMEnterpriseOperationList(Integer baseId,
			List<MEnterpriseOperation> menterpriseOperationList) {
		List<MEnterpriseOperation> newList = new ArrayList<MEnterpriseOperation>();
		for (MEnterpriseOperation record : menterpriseOperationList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				menterpriseOperationMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMEnterpriseOperation(baseId, newList);
	}

	private void updateMAttendPoliticalGroupList(Integer baseId,
			List<MAttendPoliticalGroup> mattendPoliticalGroupList) {
		List<MAttendPoliticalGroup> newList = new ArrayList<MAttendPoliticalGroup>();
		for (MAttendPoliticalGroup record : mattendPoliticalGroupList) {
			if (record.getId() == null) {
				newList.add(record);
			} else {
				record.setBaseId(baseId);
				mattendPoliticalGroupMapper.updateByPrimaryKey(record);
			}
		}
		this.insertMAttendPoliticalGroup(baseId, newList);
	}

	// --------------------------------------------------------------------------------------------

	public int deleteBaseInfoByShiBieHaoAndLeiBie(BaseInfo baseInfo) {
		BaseInfo existingRecord = this
				.getBaseInfoByShiBieHaoAndLeiBie(baseInfo);
		if (existingRecord != null) {
			return this.deleteBaseInfoById(existingRecord.getId());
		}
		return 0;
	}

	private int deleteBaseInfoById(int id) {
		return this.baseInfoMapper.deleteByPrimaryKey(id);
	}

	public int saveCountrySidePractical(
			BaseInfo baseInfo,
			CountrySidePractical countrySidePractical,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MSubtable> listB4,
			List<MResume> mresumeList,
			List<MTrainingStudy> mtrainingStudyList,
			List<MAttendMeetingAndService> mattendMeetingAndServiceList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MProductionAndManagement> mproductionAndManagementList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			countrySidePractical.setBaseId(id);
			countrySidePracticalMapper.insert(countrySidePractical);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);
			insertMSubtable(id, listB4);

			insertMResume(id, mresumeList);
			insertMTrainingStudy(id, mtrainingStudyList);
			insertMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMProductionAndManagement(id, mproductionAndManagementList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);

			return 1;
		} else {
			// TODO update
			countrySidePractical.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			countrySidePracticalMapper
					.updateByPrimaryKeySelective(countrySidePractical);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);
			updateMSubtable(id, listB4);

			updateMResume(id, mresumeList);
			updateMTrainingStudy(id, mtrainingStudyList);
			updateMAttendMeetingAndService(id, mattendMeetingAndServiceList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			updateMProductionAndManagement(id, mproductionAndManagementList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}

	}

	public int saveHainanChinese(
			BaseInfo baseInfo,
			HainanChinese hainanChinese,
			List<MSubtable> listB1,
			List<MSubtable> listB2,
			List<MSubtable> listB3,
			List<MSubtable> listB4,
			List<MSubtable> listB5,
			List<MSubtable> listB6,
			List<MResume> mresumeList,
			List<MAcademicOrganizationAndSocialGroup> macademicOrganizationAndSocialGroupList,
			List<MAttendPoliticalGroup> mattendPoliticalGroupList,
			List<MEnterpriseOperation> menterpriseOperationList,
			List<MFamily> mfamilyList, List<MActivity> mactivityList,
			List<MStory> mstoryList) throws Exception {

		Integer id = baseInfo.getId();
		if (id == null) {
			// TODO insert
			baseInfoMapper.insert(baseInfo);
			id = baseInfo.getId();
			hainanChinese.setBaseId(id);
			hainanChineseMapper.insert(hainanChinese);
			// TODO insert other data

			insertMSubtable(id, listB1);
			insertMSubtable(id, listB2);
			insertMSubtable(id, listB3);
			insertMSubtable(id, listB4);
			insertMSubtable(id, listB5);
			insertMSubtable(id, listB6);

			insertMResume(id, mresumeList);
			insertMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);
			insertMAttendPoliticalGroup(id, mattendPoliticalGroupList);
			insertMEnterpriseOperation(id, menterpriseOperationList);
			insertMFamily(id, mfamilyList);
			insertMActivity(id, mactivityList);
			insertMStory(id, mstoryList);

			return 1;
		} else {
			// TODO update
			hainanChinese.setBaseId(id);
			baseInfoMapper.updateByPrimaryKeySelective(baseInfo);
			hainanChineseMapper.updateByPrimaryKeySelective(hainanChinese);
			// TODO update other data
			updateMSubtable(id, listB1);
			updateMSubtable(id, listB2);
			updateMSubtable(id, listB3);
			updateMSubtable(id, listB4);
			updateMSubtable(id, listB5);
			updateMSubtable(id, listB6);

			updateMResume(id, mresumeList);
			updateMAcademicOrganizationAndSocialGroup(id,
					macademicOrganizationAndSocialGroupList);

			updateMAttendPoliticalGroupList(id, mattendPoliticalGroupList);
			updateMEnterpriseOperationList(id, menterpriseOperationList);
			updateMFamily(id, mfamilyList);
			updateMActivity(id, mactivityList);
			updateMStory(id, mstoryList);

			return 1;
		}

	}

	public List<BaseInfo> getAllRecord() {
		return this.baseInfoMapper.selectByExample(null);
	}

	public List<BaseInfo> getRecordsByShiBieHaoOrXingMing(String keyWord) {
		BaseInfoExample e = new BaseInfoExample();
		e.createCriteria().andShiBieHaoLike(keyWord);
		e.or().andXingMingLike(keyWord);
		return null;
	}

	public OperationManagement getOperationManagementByBaseId(Integer baseId) {
		OperationManagementExample e = new OperationManagementExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<OperationManagement> list = this.operationManagementMapper
				.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public CountrySidePractical getCountrysidePracticalByBaseId(Integer baseId) {
		CountrySidePracticalExample e = new CountrySidePracticalExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<CountrySidePractical> list = this.countrySidePracticalMapper
				.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public FolkSpecialPeople getFolkSpecialPeopleByBaseId(Integer baseId) {
		FolkSpecialPeopleExample e = new FolkSpecialPeopleExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<FolkSpecialPeople> list = this.folkSpecialPeopleMapper
				.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public HainanChinese getHainanChineseByBaseId(Integer baseId) {
		HainanChineseExample e = new HainanChineseExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<HainanChinese> list = this.hainanChineseMapper.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public HighProfessionalQualification getHighProfessionalQualificationByBaseId(
			Integer baseId) {
		HighProfessionalQualificationExample e = new HighProfessionalQualificationExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<HighProfessionalQualification> list = this.highProfessionalQualificationMapper
				.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);

	}

	public OutsideProvinceExperts getOutsideProvinceExpertsByBaseId(
			Integer baseId) {
		OutsideProvinceExpertsExample e = new OutsideProvinceExpertsExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<OutsideProvinceExperts> list = this.outsideProvinceExpertsMapper
				.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public ProfessionalSkill getProfessionalSkillByBaseId(Integer baseId) {
		ProfessionalSkillExample e = new ProfessionalSkillExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<ProfessionalSkill> list = this.professionalSkillMapper
				.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	public SocialWork getSocialWorkByBaseId(Integer baseId) {
		SocialWorkExample e = new SocialWorkExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		List<SocialWork> list = this.socialWorkMapper.selectByExample(e);
		return list.isEmpty() ? null : list.get(0);
	}

	// ------------------------------------------------------------------------------

	public List<MSubtable> getSubtableRecords(String typeFlag, Integer baseId) {
		MSubtableExample e = new MSubtableExample();
		e.createCriteria().andTypeFlagEqualTo(typeFlag)
				.andBaseIdEqualTo(baseId);
		return this.msubtableMapper.selectByExample(e);
	}

	public List<MResume> getResumePanelByBaseId(Integer baseId) {
		MResumeExample e = new MResumeExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mresumeMapper.selectByExample(e);
	}

	public List<MTrainingStudy> getTrainingStudyByBaseId(Integer baseId) {
		MTrainingStudyExample e = new MTrainingStudyExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mtrainingStudyMapper.selectByExample(e);
	}

	public List<MAttendMeetingAndService> getAttendMeetingAndServiceByBaseId(
			Integer baseId) {
		MAttendMeetingAndServiceExample e = new MAttendMeetingAndServiceExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mattendMeetingAndServiceMapper.selectByExample(e);
	}

	public List<MAcademicOrganizationAndSocialGroup> getAcademicOrganizationAndSocialGroupByBaseId(
			Integer baseId) {
		MAcademicOrganizationAndSocialGroupExample e = new MAcademicOrganizationAndSocialGroupExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.macademicOrganizationAndSocialGroupMapper
				.selectByExample(e);
	}

	public List<MProductionAndManagement> getProductionAndManagementByBaseId(
			Integer baseId) {
		MProductionAndManagementExample e = new MProductionAndManagementExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mproductionAndManagementMapper.selectByExample(e);
	}

	public List<MFamily> getFamilyByBaseId(Integer baseId) {
		MFamilyExample e = new MFamilyExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mfamilyMapper.selectByExample(e);
	}

	public List<MActivity> getActivityByBaseId(Integer baseId) {
		MActivityExample e = new MActivityExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mactivityMapper.selectByExample(e);
	}

	public List<MStory> getStoryByBaseId(Integer baseId) {
		MStoryExample e = new MStoryExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mstoryMapper.selectByExample(e);
	}

	public List<MAttendPoliticalGroup> getAttendPoliticalGroupByBaseId(
			Integer baseId) {
		MAttendPoliticalGroupExample e = new MAttendPoliticalGroupExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mattendPoliticalGroupMapper.selectByExample(e);
	}

	public List<MEnterpriseOperation> getEnterpriseOperationByBaseId(
			Integer baseId) {
		MEnterpriseOperationExample e = new MEnterpriseOperationExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.menterpriseOperationMapper.selectByExample(e);
	}

	public List<MProfessionalSkillService> getProfessionalSkillServiceByBaseId(
			Integer baseId) {
		MProfessionalSkillServiceExample e = new MProfessionalSkillServiceExample();
		e.createCriteria().andBaseIdEqualTo(baseId);
		return this.mprofessionalSkillServiceMapper.selectByExample(e);
	}

	public List<BaseInfo> getBaseInfoByLeiBie(String telentsLabel) {
		BaseInfoExample e = new BaseInfoExample();
		e.createCriteria().andLeiBieEqualTo(telentsLabel);
		return this.baseInfoMapper.selectByExample(e);
	}

}
